package com.penghk.hangzhouhouseprice.handler;

import com.penghk.hangzhouhouseprice.entity.AreaPrice;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 链家二手房价信息
 * @author penghk
 */
@Component
public class LianjiaSecondAreaPriceHandler {
    private Logger logger = LoggerFactory.getLogger(LianjiaSecondAreaPriceHandler.class);

    private List<Integer> prices = new ArrayList<>();

    public static final String xihu = "https://hz.lianjia.com/ershoufang/xihu/";
    public static final String xiacheng = "https://hz.lianjia.com/ershoufang/xiacheng/";
    public static final String jianggan = "https://hz.lianjia.com/ershoufang/jianggan/";
    public static final String gongshu = "https://hz.lianjia.com/ershoufang/gongshu/";
    public static final String shangcheng = "https://hz.lianjia.com/ershoufang/shangcheng/";
    public static final String binjiang = "https://hz.lianjia.com/ershoufang/binjiang/";
    public static final String yuhang = "https://hz.lianjia.com/ershoufang/yuhang/";
    public static final String xiaoshan = "https://hz.lianjia.com/ershoufang/xiaoshan/";
    public static final String xiasha = "https://hz.lianjia.com/ershoufang/xiasha/";

    /**
     * 获取西湖区房价
     * @return
     * @throws IOException
     */
    public AreaPrice getXiHuPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("西湖区");
        getPrice(xihu , areaPrice);
        return areaPrice;
    }

    /**
     * 获取下城区房价
     * @return
     */
    public AreaPrice getXiachengPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("下城区");
        getPrice(xiacheng , areaPrice);
        return areaPrice;
    }

    /**
     * 获取江干区房价
     * @return
     */
    public AreaPrice getJiangganPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("江干区");
        getPrice(jianggan , areaPrice);
        return areaPrice;
    }


    /**
     * 获取拱墅区房价
     * @return
     */
    public AreaPrice getGongShuPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("拱墅区");
        getPrice(gongshu , areaPrice);
        return areaPrice;
    }

    /**
     * 获取上城区房价
     * @return
     */
    public AreaPrice getShangchengPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("上城区");
        getPrice(shangcheng , areaPrice);
        return areaPrice;
    }


    /**
     * 获取滨江区房价
     * @return
     */
    public AreaPrice getBinjiangPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("滨江区");
        getPrice(binjiang , areaPrice);
        return areaPrice;
    }

    /**
     * 获取萧山区房价
     * @return
     */
    public AreaPrice getXiaoshanPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("萧山区");
        getPrice(xiaoshan , areaPrice);
        return areaPrice;
    }


    /**
     * 获取余杭区房价
     * @return
     */
    public AreaPrice getYuhangPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("余杭区");
        getPrice(yuhang , areaPrice);
        return areaPrice;
    }

    /**
     * 获取下沙区房价
     * @return
     */
    public AreaPrice getXiashaPrice() {
        AreaPrice areaPrice = new AreaPrice();
        areaPrice.setAddress("下沙区");
        getPrice(xiasha , areaPrice);
        return areaPrice;
    }

    private String getPrice(String url , AreaPrice areaPrice) {
        long priceSum = 0L;

        try {
            Document doc = Jsoup.connect(url).get();
            Elements numEle = doc.select("h2.total span");
            String num = numEle.get(0).text().trim();
            int count = Integer.parseInt(num);
            logger.info("count : "+count);

            Elements head = doc.select("div.unitPrice span");//30 per page
            int perPageNum = head.size();
            int pages = count / perPageNum;
            int offset = count % perPageNum;
            if (offset > 0) {
                pages++;
            }

            for (int i = 1 ; i <= pages ;i++) {
                String conurl = url+"pg"+i+"/";
                logger.info("page ："+i+"  url : "+conurl);
                Document nextDoc = Jsoup.connect(conurl).get();
                Elements priceEle = nextDoc.select("div.unitPrice span");//30 per page
                for (Element element : priceEle) {
                    logger.info(element.text());
                    String priceStr = element.text();
                    int endIndex = priceStr.indexOf("元");
                    if (endIndex < 0) {
                        continue;
                    }
                    priceStr = priceStr.substring(2 , endIndex);
                    int sinPrice = Integer.parseInt(priceStr);
                    if(sinPrice > 200000 || sinPrice < 6000) {
                        continue;
                    }
                    prices.add(sinPrice);
                    priceSum += sinPrice;
                }
            }
            int prePrice = (int) (priceSum /prices.size());
            logger.info("size : "+prices.size());
            Collections.sort(prices);
            logger.info(" 最低价 ： "+prices.get(0)+" 均价 : "+prePrice+" 最高价 ："+prices.get(prices.size()-1));
            areaPrice.setLowPrice(String.valueOf(prices.get(0)));
            areaPrice.setMidPrice(String.valueOf(prePrice));
            areaPrice.setMaxPrice(String.valueOf(prices.get(prices.size()-1)));
            areaPrice.setNum(prices.size());

            prices.clear();
            return Integer.toString(prePrice);
        }catch (Exception e) {
            return "";
        }
    }



}
